home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Demos / A.D. Software / OOFILE 1.3b4d6.sit / OOFILE 1.3b4d6 / MacCodeWarriorDemo1.3b4d6 / docs / samples / ooftst20.cpp / ooftst20.cpp
Encoding:
C/C++ Source or Header  |  1997-04-03  |  3.8 KB  |  156 lines  |  [TEXT/CWIE]

  1. // Copyright 1994 A.D. Software. All Rights Reserved
  2.  
  3. // OOFTEST20
  4.  
  5. // This sample demonstrates how to create an ad-hoc query.
  6.  
  7. // Simple stream I/O is used to interact with the user.
  8.  
  9. #include "oofile.h"
  10. #include "ooftst01.h"
  11.  
  12. void anyName(const char* searchStrings[], bool isOr, unsigned short numSch);
  13. void lastName(const char* searchStrings[], unsigned short numSch);
  14.  
  15. #define numEntries(A) sizeof(A)/sizeof(A[0])
  16.  
  17.     TEST_CONNECT    theDB;
  18.     dbPeople     People;
  19.  
  20.  
  21. int main()
  22. {
  23.     cout << "OOFILE Validation Suite - Test 20\n"
  24.          << "Demonstration of ad-hoc query building\n";
  25.     
  26. // bit of complicated filename logic to support different backends
  27. // with this one test program
  28.  
  29. #ifdef TESTING_CTREE
  30. // we can use separate files, or a single container file
  31. // and this sample uses separate files
  32.     theDB.useSeparateFiles();
  33.  
  34.     #ifdef _Macintosh
  35. // path handling only on the Mac at present
  36.         const char* kExistsName =  ":ooftst01:People.dat";
  37.         const char* kDatabaseName = ":ooftst01:";
  38.     #else
  39.         const char* kExistsName =   "People.dat"
  40.         const char* kDatabaseName = "";
  41.     #endif    
  42.  
  43. #else
  44.     #ifdef TESTING_DBASE
  45.         #ifdef _Macintosh
  46.             const char* kExistsName =  ":ooftst01:People.dbf";
  47.             const char* kDatabaseName = ":ooftst01:";
  48.         #else
  49.             const char* kExistsName =   "People.dbf"
  50.             const char* kDatabaseName = "";
  51.         #endif    
  52.  
  53.     #else
  54.         // Persistent RAM, no option of single file
  55.         #ifdef _Macintosh
  56.             const char* kDatabaseName = ":ooftst01:ooftst01.db";
  57.         #else
  58.             const char* kDatabaseName = "ooftst01.db";
  59.         #endif    
  60.         const char* kExistsName = kDatabaseName;
  61.     #endif
  62. #endif
  63.  
  64. // open or create the database    
  65.     if (dbConnect::fileExists(kExistsName)) {
  66.         theDB.openConnection(kDatabaseName);
  67.         cout << "Just opened...\n" << theDB << endl << endl;
  68.     }
  69.     else {
  70.         theDB.newConnection(kDatabaseName);
  71.         People.AddTestData();
  72.     }
  73.  
  74.     People.selectAll();
  75.     cout << "Listing records\n" << (dbView(People) << People.LastName << People.OtherNames);
  76.  
  77.     const char* userEntries[] = {"Dent", "Andy"};    
  78.     anyName(userEntries, true, numEntries(userEntries));
  79.     anyName(userEntries, false, numEntries(userEntries));
  80.  
  81.     const char* userEntries2[] = {"Dent", "Tay*"};    
  82.     lastName(userEntries2, numEntries(userEntries));
  83.     cout << "Test Completed" << endl;
  84.     
  85.     return EXIT_SUCCESS;
  86.  
  87.  
  88. void
  89. anyName(const char* searchStrings[], bool isOr, unsigned short numSch)
  90. {
  91.     dbQuery theQuery;
  92.     if (isOr)
  93.         cout << "OR";
  94.     else
  95.         cout << "AND";
  96.     cout << " Querying either Last or Othernames = \n";
  97.     
  98.     for (unsigned short i=0; i<numSch; i++) {
  99.         cout << "   " << searchStrings[i] << endl;
  100.  
  101. // construct clauses on the heap otherwise they are temporaries
  102. // which won't exist by the time we do the search()
  103.         
  104.         dbQueryClause* term1 = new dbQueryBinary(
  105.             People.LastName == searchStrings[i]
  106.         );
  107.         dbQueryClause* term2 = new dbQueryBinary(
  108.             People.OtherNames == searchStrings[i]
  109.         );
  110.  
  111. // the following is a bit subtle - the operator||
  112. // which takes pointers creates a dbQueryBinaryComboOwner
  113. // which will delete the sub-clauses
  114.         if (isOr) {
  115. //            theQuery |= (*term1 || term2); equivalent to the following
  116.             theQuery |= term1;
  117.             theQuery |= term2;
  118.         }
  119.         else
  120.             theQuery &= (*term1 || term2);
  121.     }
  122.     People.search(theQuery);
  123.  
  124.     cout << "\nListing result of query\n" 
  125.          << (dbView(People) << People.LastName << People.OtherNames) << endl;
  126.  
  127. }
  128.  
  129.  
  130.  
  131. void
  132. lastName(const char* searchStrings[], unsigned short numSch)
  133. {
  134.     dbQuery theQuery;
  135.     cout << "Querying Lastname = any of\n";
  136.     
  137.     for (unsigned short i=0; i<numSch; i++) {
  138.         cout << "   " << searchStrings[i] << endl;
  139.  
  140. // construct clauses on the heap otherwise they are temporaries
  141. // which won't exist by the time we do the search()
  142.         
  143.         dbQueryClause* theTerm = new dbQueryBinary(
  144.             People.LastName == searchStrings[i]
  145.         );
  146.  
  147.         theQuery |= theTerm;
  148.     }
  149.     People.search(theQuery);
  150.  
  151.     cout << "\nListing result of query\n" 
  152.          << (dbView(People) << People.LastName << People.OtherNames) << endl;
  153.  
  154. }
  155.